//
//  HWInviteFriendController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/1.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWInviteFriendController: HWBaseViewController {
    var navRightButton:UIButton!
    var navRightButtonTwo:UIButton!
    var bgScrollView:UIScrollView!
    var bgView:UIView!
    var topBgImageView:UIImageView!
    var centerBgView:UIImageView!
    var price:UILabel!
    var priceLabel:UILabel!
    var vLineView:UIView!
    var bonus:UILabel!
    var bonusLabel:UILabel!
    var sendButton:UIButton!
    var hLineView:UIView!
    var inviteTitle:UILabel!
    var inviteBg:UIImageView!
    var inviteLabel:UILabel!
    var copyButton:UIButton!
    var inviteFlowPathTitle:UILabel!
    var inviteFlowPathBg:UIImageView!
    var qrcodeCodeImageView:UIImageView!
    var qrContentLabel:UILabel!
    var inviteBottomBg:UIImageView!
    
    var bottomInvBg:UIView!
    var bottomInvTitle:UILabel!
    var bottomInvNum:UILabel!
    
    var bottomCoinBg:UIView!
    var bottomCoinTitle:UILabel!
    var bottomCoinNum:UILabel!
    
    var bottomScoreBg:UIView!
    var bottomScoreTitle:UILabel!
    var bottomScoreNum:UILabel!
    
    var recordButton:UIButton!
    
    var shuoming = ""
    var link = ""
    var httpManger = HWInviteHttpManger()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
    }
    

}

extension HWInviteFriendController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = ""
        
        navRightButton = UIButton(type: .custom)
//        navRightButton.setImage(UIImage(named: "inviateRecordIcon"), for: .normal)
        navRightButton.setImage(UIImage(named: "vipNavRight"), for: .normal)
        navRightButton.addTarget(self, action: #selector(navRightButtonClick(_:)), for: .touchUpInside)
        navigationView.addSubview(navRightButton)
        
        navRightButtonTwo = UIButton(type: .custom)
        navRightButtonTwo.setImage(UIImage(named: "vipNavRight"), for: .normal)
        navRightButtonTwo.addTarget(self, action: #selector(navRightButtonTwoClick(_:)), for: .touchUpInside)
        navRightButtonTwo.isHidden = true
        navigationView.addSubview(navRightButtonTwo)
        
        bgScrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        bgScrollView.contentInsetAdjustmentBehavior = .never
        bgScrollView.bounces = false
        bgScrollView.showsVerticalScrollIndicator = false
        view.addSubview(bgScrollView)

        bgView = UIView()
        bgView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 1246*widthScale)
        bgScrollView.addSubview(bgView)
        
        UIColor.gradientColor(view: bgView, colors: [UIColor.colorWithHexString(hexString: "#F9B959").cgColor,
                                                   UIColor.colorWithHexString(hexString: "#FF756C").cgColor,
                                                   UIColor.colorWithHexString(hexString: "#FF6451").cgColor,
                                                   UIColor.colorWithHexString(hexString: "#FFA985").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: 1))
        view.bringSubviewToFront(navigationView)
        
        topBgImageView = UIImageView()
        topBgImageView.image = UIImage(named: "inviateTopBg")
        bgScrollView.addSubview(topBgImageView)
        
        centerBgView = UIImageView()
        centerBgView.image = UIImage(named: "inviateCenterBg")
        centerBgView.isUserInteractionEnabled = true
        bgScrollView.addSubview(centerBgView)
        
        price = HWLabel.createBoldLabel(title: "", color: "#FF726A", font: 22*widthScale)
        price.textAlignment = .center
        centerBgView.addSubview(price)
        
        priceLabel = HWLabel.createLabel(title: "累充金额", color: "#333333", font: 13*widthScale)
        priceLabel.textAlignment = .center
        centerBgView.addSubview(priceLabel)
        
        vLineView = UIView()
        vLineView.backgroundColor = .colorWithHexString(hexString: "#EEEEEE")
        centerBgView.addSubview(vLineView)
        
        bonus = HWLabel.createBoldLabel(title: "", color: "#FF726A", font: 22*widthScale)
        bonus.textAlignment = .center
        centerBgView.addSubview(bonus)
        
        bonusLabel = HWLabel.createLabel(title: "激励奖金", color: "#333333", font: 13*widthScale)
        bonusLabel.textAlignment = .center
        centerBgView.addSubview(bonusLabel)
        
        sendButton = UIButton(type: .custom)
        sendButton.setImage(UIImage(named: "inviateFriendSendButton"), for: .normal)
        sendButton.addTarget(self, action: #selector(sendButtonClick(_:)), for: .touchUpInside)
        centerBgView.addSubview(sendButton)
        
        hLineView = UIView()
        hLineView.frame = CGRect(x: 0, y: 0, width: 290*widthScale, height: 1*widthScale)
        centerBgView.addSubview(hLineView)
        hLineView.drawDashLine(hLineView, strokeColor: UIColor.colorWithHexString(hexString: "#EEEEEE"), lineWidth: 1, lineLength: 5, lineSpacing: 2)
        
        inviteTitle = HWLabel.createLabel(title: "我的邀请码", color: "#333333", font: 13*widthScale)
        centerBgView.addSubview(inviteTitle)
        
        inviteBg = UIImageView()
        inviteBg.image = UIImage(named: "inviateCodeBg")
        centerBgView.addSubview(inviteBg)
        
        inviteLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 22*widthScale)
        inviteBg.addSubview(inviteLabel)
        
        copyButton = UIButton(type: .custom)
        copyButton.setTitle("复制", for: .normal)
        copyButton.titleLabel?.font = .boldSystemFont(ofSize: 16*widthScale)
        copyButton.frame = CGRect(x: 0, y: 0, width: 290*widthScale, height: 44*widthScale)
        copyButton.layer.masksToBounds = true
        copyButton.layer.cornerRadius = 22*widthScale
        copyButton.addTarget(self, action: #selector(copyButtonClick(_:)), for: .touchUpInside)
        centerBgView.addSubview(copyButton)
        
        UIColor.gradientColor(view: copyButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,
                                                             UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        recordButton = UIButton(type: .custom)
        recordButton.setTitle("邀请记录", for: .normal)
        recordButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: .normal)
        recordButton.titleLabel?.font = .systemFont(ofSize: 13*widthScale)
        recordButton.addTarget(self, action: #selector(recordButtonClick(_:)), for: .touchUpInside)
        centerBgView.addSubview(recordButton)
        
        inviteFlowPathTitle = HWLabel.createBoldLabel(title: "福利获取流程", color: "#FFFFFF", font: 14*widthScale)
        bgScrollView.addSubview(inviteFlowPathTitle)
        
        inviteFlowPathBg = UIImageView()
        inviteFlowPathBg.image = UIImage(named: "inviateFlowPath")
        bgScrollView.addSubview(inviteFlowPathBg)
        
        qrcodeCodeImageView = UIImageView()
        qrcodeCodeImageView.layer.cornerRadius = 18*widthScale
        qrcodeCodeImageView.layer.masksToBounds = true
        qrcodeCodeImageView.isUserInteractionEnabled = true
        bgScrollView.addSubview(qrcodeCodeImageView)
        
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressImage(_:)))
        qrcodeCodeImageView.addGestureRecognizer(longPress)
        
        qrContentLabel = HWLabel.createBoldLabel(title: "长按二维码图片保存\n可分享给好友进行下载", color: "#FFFFFF", font: 14*widthScale)
        qrContentLabel.textAlignment = .center
        bgScrollView.addSubview(qrContentLabel)
        
        inviteBottomBg = UIImageView()
        inviteBottomBg.image = UIImage(named: "inviateBottomBg")
        bgScrollView.addSubview(inviteBottomBg)
        
        bottomInvBg = UIView()
        bottomInvBg.backgroundColor = .white
        bottomInvBg.layer.cornerRadius = 18*widthScale
        inviteBottomBg.addSubview(bottomInvBg)
        
        bottomInvTitle = HWLabel.createLabel(title: "邀请好友", color: "#333333", font: 13*widthScale)
        bottomInvTitle.textAlignment = .center
        bottomInvBg.addSubview(bottomInvTitle)
        
        bottomInvNum = HWLabel.createBoldLabel(title: "", color: "#FF726A", font: 24*widthScale)
        bottomInvNum.textAlignment = .center
        bottomInvBg.addSubview(bottomInvNum)
        
        bottomCoinBg = UIView()
        bottomCoinBg.backgroundColor = .white
        bottomCoinBg.layer.cornerRadius = 18*widthScale
        inviteBottomBg.addSubview(bottomCoinBg)
        
        bottomCoinTitle = HWLabel.createLabel(title: "获得平台币", color: "#333333", font: 13*widthScale)
        bottomCoinTitle.textAlignment = .center
        bottomCoinBg.addSubview(bottomCoinTitle)
        
        bottomCoinNum = HWLabel.createBoldLabel(title: "", color: "#FF726A", font: 24*widthScale)
        bottomCoinNum.textAlignment = .center
        bottomCoinBg.addSubview(bottomCoinNum)
        
        bottomScoreBg = UIView()
        bottomScoreBg.backgroundColor = .white
        bottomScoreBg.layer.cornerRadius = 18*widthScale
        inviteBottomBg.addSubview(bottomScoreBg)
        
        bottomScoreTitle = HWLabel.createLabel(title: "获得积分", color: "#333333", font: 13*widthScale)
        bottomScoreTitle.textAlignment = .center
        bottomScoreBg.addSubview(bottomScoreTitle)
        
        bottomScoreNum = HWLabel.createBoldLabel(title: "", color: "#FF726A", font: 24*widthScale)
        bottomScoreNum.textAlignment = .center
        bottomScoreBg.addSubview(bottomScoreNum)
    }
    
    private func initFrame() -> Void {
        
        navRightButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.width.height.equalTo(23*widthScale)
            make.centerY.equalTo(navigationView.titleLabel)
        }
        
        navRightButtonTwo.snp.makeConstraints { make in
            make.right.equalTo(navRightButton.snp.left).offset(-18*widthScale)
            make.width.height.equalTo(23*widthScale)
            make.centerY.equalTo(navigationView.titleLabel)
        }
        
        bgScrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        bgView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
            make.height.equalTo(1246*widthScale)
        }
        
        topBgImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(86*widthScale)
            make.height.equalTo(313*widthScale)
        }
        
        centerBgView.snp.makeConstraints { make in
            make.left.equalTo(11*widthScale)
            make.right.equalTo(-11*widthScale)
            make.height.equalTo(308*widthScale)
            make.top.equalTo(332*widthScale)
        }
        
        vLineView.snp.makeConstraints { make in
            make.left.equalTo(124*widthScale)
            make.top.equalTo(37*widthScale)
            make.height.equalTo(32*widthScale)
            make.width.equalTo(1*widthScale)
        }
        
        price.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.right.equalTo(vLineView.snp.left)
            make.top.equalTo(31*widthScale)
            make.height.equalTo(30*widthScale)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.left.right.equalTo(price)
            make.height.equalTo(18*widthScale)
            make.top.equalTo(price.snp.bottom).offset(2*widthScale)
        }
        
        sendButton.snp.makeConstraints { make in
            make.right.equalTo(-32*widthScale)
            make.top.equalTo(42*widthScale)
            make.width.equalTo(54*widthScale)
            make.height.equalTo(29*widthScale)
        }
        
        bonus.snp.makeConstraints { make in
            make.right.equalTo(sendButton.snp.left)
            make.left.equalTo(vLineView.snp.right)
            make.top.equalTo(31*widthScale)
            make.height.equalTo(30*widthScale)
        }
        
        bonusLabel.snp.makeConstraints { make in
            make.left.right.equalTo(bonus)
            make.height.equalTo(18*widthScale)
            make.top.equalTo(bonus.snp.bottom).offset(2*widthScale)
        }
        
        hLineView.snp.makeConstraints { make in
            make.left.equalTo(32*widthScale)
            make.right.equalTo(-32*widthScale)
            make.height.equalTo(1*widthScale)
            make.top.equalTo(114*widthScale)
        }
        
        inviteTitle.snp.makeConstraints { make in
            make.height.equalTo(18*widthScale)
            make.top.equalTo(hLineView.snp.bottom).offset(14*widthScale)
            make.centerX.equalToSuperview()
        }
        
        inviteBg.snp.makeConstraints { make in
            make.left.equalTo(32*widthScale)
            make.right.equalTo(-32*widthScale)
            make.top.equalTo(inviteTitle.snp.bottom).offset(9*widthScale)
        }
        
        inviteLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        copyButton.snp.makeConstraints { make in
            make.left.equalTo(32*widthScale)
            make.right.equalTo(-32*widthScale)
            make.top.equalTo(inviteBg.snp.bottom).offset(9*widthScale)
            make.height.equalTo(44*widthScale)
        }
        
        recordButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(copyButton.snp.bottom).offset(14*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        inviteFlowPathTitle.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(centerBgView.snp.bottom)
            make.height.equalTo(20*widthScale)
        }
        
        inviteFlowPathBg.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(inviteFlowPathTitle.snp.bottom).offset(9*widthScale)
        }
        
        qrcodeCodeImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(inviteFlowPathBg.snp.bottom).offset(18*widthScale)
            make.width.height.equalTo(185*widthScale)
        }
        
        qrContentLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(qrcodeCodeImageView.snp.bottom).offset(9*widthScale)
            make.width.equalTo(174*widthScale)
            make.height.equalTo(40*widthScale)
        }
        
        inviteBottomBg.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.height.equalTo(121*widthScale)
            make.top.equalTo(qrContentLabel.snp.bottom).offset(18*widthScale)
        }
        
        bottomInvBg.snp.makeConstraints { make in
            make.left.top.equalTo(9*widthScale)
            make.width.height.equalTo(103*widthScale)
        }
        
        bottomInvTitle.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(22*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        bottomInvNum.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(bottomInvTitle.snp.bottom).offset(9*widthScale)
            make.height.equalTo(34*widthScale)
        }
        
        bottomCoinBg.snp.makeConstraints { make in
            make.top.equalTo(9*widthScale)
            make.left.equalTo(bottomInvBg.snp.right).offset(9*widthScale)
            make.width.height.equalTo(103*widthScale)
        }
        
        bottomCoinTitle.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(22*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        bottomCoinNum.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(bottomCoinTitle.snp.bottom).offset(9*widthScale)
            make.height.equalTo(34*widthScale)
        }
        
        bottomScoreBg.snp.makeConstraints { make in
            make.top.equalTo(9*widthScale)
            make.left.equalTo(bottomCoinBg.snp.right).offset(9*widthScale)
            make.width.height.equalTo(103*widthScale)
        }
        
        bottomScoreTitle.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(22*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        bottomScoreNum.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(bottomScoreTitle.snp.bottom).offset(9*widthScale)
            make.height.equalTo(34*widthScale)
        }
    }
}


extension HWInviteFriendController {
    func loadData() -> Void {
        httpManger.getInviteInfo { model in
            self.organizeData(model)
        } failure: { errorString in
            
        }

    }
}

extension HWInviteFriendController {
    func organizeData(_ model:HWInviteModel) -> Void {
        price.text = model.pay
        priceLabel.text = model.pay_title
        bonus.text = model.jiangjin
        bonusLabel.text = model.jiangjin_title
        inviteLabel.text = model.invite_code
        qrcodeCodeImageView.kf.setImage(with: URL(string: model.qrcode))
//        qrcodeCodeImageView.image = UIImage.createQRCodeImage(content: model.qrcode, size: CGSize(width: 150*widthScale, height: 150*widthScale))
        bottomInvNum.text = model.num
        bottomCoinNum.text = model.coin
        bottomScoreNum.text = model.points
        shuoming = model.note
        shuoming = HWUniversal.base64Decoding(encodedString: shuoming.removingPercentEncoding ?? "")
        link = model.link
        var tempLogo:UIImageView!
        for mod in model.liucheng {
            let logo = UIImageView()
            logo.kf.setImage(with: URL(string: mod.icon))
            inviteFlowPathBg.addSubview(logo)
            
            let titleLabel = HWLabel.createLabel(title: mod.title, color: "#FFFFFF", font: 14*widthScale)
            inviteFlowPathBg.addSubview(titleLabel)
            
            if tempLogo != nil{
                logo.snp.makeConstraints { make in
                    make.left.equalTo(27*widthScale)
                    make.top.equalTo(tempLogo.snp.bottom).offset(18*widthScale)
                    make.width.height.equalTo(24*widthScale)
                }
            } else {
                logo.snp.makeConstraints { make in
                    make.left.equalTo(27*widthScale)
                    make.top.equalTo(18*widthScale)
                    make.width.height.equalTo(24*widthScale)
                }
            }
            
            titleLabel.snp.makeConstraints { make in
                make.left.equalTo(logo.snp.right).offset(14*widthScale)
                make.right.equalTo(-10*widthScale)
                make.centerY.equalTo(logo)
                make.height.equalTo(20*widthScale)
            }
            
            tempLogo = logo
        }
    }
}

extension HWInviteFriendController {
    @objc func navRightButtonClick(_ :UIButton) -> Void {
        if shuoming.count > 0 {
            let alert = HWInviteAlertView()
            let htmlString:NSMutableAttributedString = (self.shuoming.toHtmlAttributedText() ?? NSMutableAttributedString())
            alert.contentLabel.attributedText = htmlString
            alert.show()
        }
    }
    
    @objc func navRightButtonTwoClick(_ :UIButton) -> Void {
        let record = HWMoWanVouchersViewController()
        self.navigationController?.pushViewController(record, animated: true)
    }
    
    @objc func copyButtonClick(_ :UIButton) -> Void {
        if self.link.count > 0 {
            let pasteBoard =  UIPasteboard.general
            pasteBoard.string = self.link
            self.view.showHUD(title: "复制成功")
        }
    }
    
    @objc func sendButtonClick(_:UIButton) -> Void {
        self.navigationController?.pushViewController(HWInviteRewardController(), animated: true)
    }
    
    @objc func recordButtonClick(_:UIButton) -> Void {
        let record = HWMoWanVouchersViewController()
        self.navigationController?.pushViewController(record, animated: true)
    }
    
    @objc func longPressImage(_:UILongPressGestureRecognizer) -> Void {
        let alert = UIAlertController(title: "提示", message: "是否要保存二维码到相册", preferredStyle: .alert)
        let sure = UIAlertAction(title: "确定", style: .default) { act in
            UIImageWriteToSavedPhotosAlbum(self.qrcodeCodeImageView.image ?? UIImage(), self, #selector(self.image(image:didFinishSavingWithError:contextInfo:)), nil)
        }
        
        let cancel = UIAlertAction(title: "取消", style: .default) { act in
            
        }
        
        alert.addAction(cancel)
        alert.addAction(sure)
        self.present(alert, animated: true)
        
    }
    
    @objc private func image(image: UIImage,didFinishSavingWithError: NSError?,contextInfo: Any){
            var showInfo = ""
            if didFinishSavingWithError != nil {
                showInfo = "保存失败"
            }else{
                showInfo = "保存成功"
            }
        view.showHUD(title: showInfo)
            
        }

    
}
